﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DBAccessor.Accessor;
using AlanUtilities;
using DBList.Entities;
using DBList;
using System.Collections.Generic;

public partial class TakeLiJuan : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		String strUID = OperateControl.PickupAvaiableUserID(this);
		if (!IsPostBack) {
			int nUID, nOperateID;
			int.TryParse(strUID, out nUID);
			string strOpeTookID = Request.QueryString.Get("ArticleId");
			if (strOpeTookID == null)
			{
				Session[@"LogUserId"] = null;
				OperateControl.PickupAvaiableUserID(this);
				return;
			}
			MemberAccessor memberSystem = new MemberAccessor();
			UserData usrData = memberSystem.GetMemberInfo(nUID);
			if (usrData.GroupId != 1 && usrData.GroupId != 2)
			{
				Session[@"LogUserId"] = null;
				OperateControl.PickupAvaiableUserID(this);
				return;
			}
			nOperateID = nUID;

			int.TryParse(strOpeTookID, out nUID);
			operaterLabel.Text = strOpeTookID;

			BindDrpCategory(nOperateID);
			if(nUID == 0){
				opeSrcKind.Text = "";

				SelKindDrpCategory(0);
				SelMenDianDrpCategory(0);
			}
			else {
				CPNAccessor cpnAcce = new CPNAccessor();
				TakerKindData tkd = cpnAcce.GetTakerKindInfo(nUID);

				opeSrcKind.Text = tkd.KindID.ToString();
				SelKindDrpCategory(tkd.KindID);
				SelMenDianDrpCategory(tkd.MemberID);
			}
		}
	}
	private	void SelMenDianDrpCategory(int nMemberID)
	{
		UserData selusrData = null;
		if (nMemberID != 0){
			MemberAccessor memberSystem = new MemberAccessor();
			UserData usrData = memberSystem.GetMemberInfo(nMemberID);
			foreach (ListItem li in drpMenDian.Items)
			{
				if (li.Text == usrData.Introduction && li.Value == usrData.MemberId.ToString())
				{
					drpMenDian.SelectedValue = li.Value;
					selusrData = usrData;
					break;
				}
			}
		}
		if (selusrData == null){
			ListItem drpLI = drpMenDian.SelectedItem;
			nMemberID = int.Parse(drpLI.Value);

			MemberAccessor memberSystem = new MemberAccessor();
			selusrData = memberSystem.GetMemberInfo(nMemberID);
		}
		if(selusrData != null){
			tbAccount.Text = selusrData.MemberName;
			tbRealName.Text = selusrData.RealName;
			tbUserPhone.Text = selusrData.Phone;
			if (selusrData.Email != null && selusrData.Email.Length > 0)
			{
				if (tbUserPhone.Text.Length > 0)
				{
					tbUserPhone.Text += "/";
					tbUserPhone.Text += selusrData.Email;
				}
				else tbUserPhone.Text = selusrData.Email;
			}
			tbAddress.Text = selusrData.Address;
		}
	}
	private	void SelKindDrpCategory(int nKindID)
	{
		ClassData selclsData = null;
		CPNAccessor accessor = new CPNAccessor();
		if (nKindID != 0)
		{
			ClassData clsData = accessor.GetGoodsKindInfo(nKindID);
			foreach (ListItem li in ddlGiftKind.Items){
				if (li.Text == clsData.ClassName && li.Value == clsData.ClassId.ToString()){
					ddlGiftKind.SelectedValue = li.Value;
					selclsData = clsData;
					break;
				}
			}
		}
		if (selclsData == null)
		{
			ListItem ddlLI = ddlGiftKind.SelectedItem;
			nKindID = int.Parse(ddlLI.Value);

			selclsData = accessor.GetGoodsKindInfo(nKindID);
		}
		if (selclsData != null)
		{
			tbSingleMoney.Text = selclsData.ClassMoney.ToString();

			int	nOpeTookID = 0;
			int.TryParse(operaterLabel.Text, out nOpeTookID);
			if(nKindID > 0 && nKindID.ToString() == opeSrcKind.Text && nOpeTookID > 0)
			{
				LYDanData lydData = accessor.GetLYDanData(nOpeTookID);
				tbBeginNum.Text = lydData.BeginNumber.ToString();
				tbEndNum.Text = lydData.EndNumber.ToString();
				tbTotalNum.Text = lydData.TotalNumber.ToString();
			}
			else {
				tbBeginNum.Text = "";
				if(nKindID > 0){
					int nRet = accessor.GetTookListKindMaxInt(nKindID);
					tbBeginNum.Text = nRet.ToString();
				}
				tbTotalNum.Text = "";
				tbEndNum.Text = "";
			}
		}
	}
	protected void BindDrpCategory(int	nOperateID)
	{
		ddlGiftKind.Items.Add(new ListItem(@"请选择种类", "0"));
		CPNAccessor accessor = new CPNAccessor();
		List<ClassData> list = accessor.GetAllKind(nOperateID);
		foreach (ClassData clsData in list)
		{
			ddlGiftKind.Items.Add(new ListItem(clsData.ClassName, clsData.ClassId.ToString()));
		}

		drpMenDian.Items.Add(new ListItem(@"请选择门店", "0"));
		MemberAccessor memAcce = new MemberAccessor();
		int		nTotal;
		List<UserData> userList = memAcce.GetAllMemberData(1, Int16.MaxValue, out nTotal, null, null);
		foreach (UserData userData in userList)
		{
			if (userData.GroupId == 5){
				drpMenDian.Items.Add(new ListItem(userData.Introduction, userData.MemberId.ToString()));
			}
		}
	}
	protected void ddlGiftKind_SelectedIndexChanged(object sender, EventArgs e)
	{
		SelKindDrpCategory(0);
	}
	protected void drpMenDian_SelectedIndexChanged(object sender, EventArgs e)
	{
		SelMenDianDrpCategory(0);
	}
	protected void btnSubmit_Click(object sender, EventArgs e)
	{
		Label2.Text = @"";
		string strBeginInt = tbBeginNum.Text.Trim();
		string strEndInt = tbEndNum.Text.Trim();
		int	nBeginInt = 0, nEndInt = 0;
		int.TryParse(strBeginInt, out nBeginInt);
		int.TryParse(strEndInt, out nEndInt);
		if (nEndInt < nBeginInt || nBeginInt < 1)
		{
			tbTotalNum.Text = "";
			Label2.Text = @"结束数必需比开始数大且开始数大于0！";
			return;
		}
		int nTotalNum = nEndInt - nBeginInt + 1;
		tbTotalNum.Text = nTotalNum.ToString();
		ListItem ddlLI = ddlGiftKind.SelectedItem;
		ListItem drpLI = drpMenDian.SelectedItem;
		int nKindID, nMemberID;
		nKindID = int.Parse(ddlLI.Value);
		nMemberID = int.Parse(drpLI.Value);
		if(nKindID == 0 || nMemberID == 0){
			Label2.Text = @"请选择种类和门店！";
			return;
		}
		CPNAccessor accessor = new CPNAccessor();
		String strTmpID = OperateControl.PickupAvaiableUserID(this);
		int nOpeId = int.Parse(strTmpID);
		int nOpeTookID = 0, nOldMemberID = nMemberID;
		int.TryParse(operaterLabel.Text, out nOpeTookID);
		if (nOpeTookID != 0){
			TakerKindData tkd = accessor.GetTakerKindInfo(nOpeTookID);
			nOldMemberID = tkd.MemberID;
		}
		int nRet = accessor.IsMultiTakerExist(nKindID, nBeginInt, nEndInt, nOldMemberID.ToString());
		if (nRet == 10){
			Label2.Text = @"选择的种类段不存在！";
			return;
		}
		else if(nRet > 0){
			Label2.Text = @"选择的号码段有被其他人领用过！";
			return;
		}
		else if (nRet < 0)
		{
			Label2.Text = @"异常，请稍后重试！";
			return;
		}
		nRet = accessor.ActivateBarCode(nOpeId, nKindID, nBeginInt, nEndInt, nMemberID, null, nOpeTookID);
		if(nRet == 20){
			Response.Redirect("LinYongList.aspx");
		}
		else {
			Label2.Text = @"操作失败！该段条码可能作废或已被使用！";
		}
	}
}
